import {
	request
} from '../utils/request.js'
import store from '../store'
import {
	Toast,
	Jump
} from '../utils/tools.js'
import md5 from 'js-md5'
import Vue, {
	warn
} from 'vue'
import cookie from '@/utils/auth.js'
import {
	GetShopCar
} from './Goods.js'
import {
	AESencrypt,
	AESdecrypt
} from "@/libs/fn/mima.js"
// import { getcoindefine } from './ctrls.js'
import {
	toWei,
	fromWei
} from "@/utils/tools.js"

//登陆
export async function Login_Phone(params, type = 1) {
	let a = {
		...params,
		password: AESencrypt(params.password)
	}
	try {
		//不加密
		if (type == 1) {
			params.password = AESencrypt(params.password)
		}
		let {
			success,
			msg,
			data
		} = await request({
			url: '/Users/Login_Phone',
			method: "post",
			params,
		})
		Toast(msg);
		if (success) {
			store.commit('user/SET_BACKPUBLIC', {})
			store.commit('user/SET_BACKSELF', {})
			store.commit('address/CLEAR')
			store.commit("user/SET_TOKEN", data);
			// Vue.prototype.Ws.reconnect();
			//地址
			GetMyAddress();
			//购物车数据
			// GetShopCar();
			// 获取银行卡列表
			banklist()
			let res = await UserInfo();
			console.log('res====>', res.data)
			let b = {
				...a,
				token: data,
				headpicurl: res.data.headpicurl,
				username: res.data.username,
				lastlogintime: res.data.lastlogintime,
				nickname: res.data.nickname,
				userid: res.data.userid
			}
			let c = cookie.get('userlist') ? JSON.parse(cookie.get('userlist')) : [];
			// 去重
			c = c.filter(item => item.Phone != b.Phone)
			c.push(b)
			cookie.set('userlist', JSON.stringify(c))
			// savaAccountlist({
			// 	address: address,
			// 	username: username,
			// 	mail: mail,
			// 	password: params.password,
			// })
			//成功
			Jump({
				url: "/pages/farm/index",
				mode: 4,
			});
		}
		return success
	} catch (e) {
		console.log(e)
		return false
	}
}


// /Users/IMEI_PhoneRegister
export async function IMEI_PhoneRegister(params) {
	console.log('params===>',params)
	try {
		if (params.password) {
			params.password = AESencrypt(params.password)
		}
		let {
			success,
			data,
			data2,
			msg
		} = await request({
			url: "/Users/IMEI_PhoneRegister",
			method: "post",
			data: params,
		});
		Toast(msg)
		if (success) {
			store.commit("user/SET_TOKEN", data);
			store.commit("user/set_deciceToken", data2);
			store.commit("user/set_farmActivationCode", params.farmActivationCode);
			cookie.set('invCode', '')
			await UserInfo();
			Jump({
				url: "/pages/farm/index",
				mode: 4,
			});
		}
		return success;
	} catch (e) {
		console.log(e);
		return false;
	}
}

// 注册
export async function UserRegister(params, type = 1) {
	try {
		if (params.password) {
			params.password = AESencrypt(params.password);
		}
		let {
			success,
			data,
			msg
		} = await request({
			url: "/Users/UserRegister",
			method: "post",
			params,
		});
		Toast(msg)

		if (success) {
			store.commit("user/SET_TOKEN", data);
			cookie.set('invCode', '')
			await UserInfo();
			// Vue.prototype.Ws.reconnect();
			// 注册成功
			if (type == 1) {
				Jump({
					url: "/pages/farm/index",
					mode: 4,
				});
			} else {
				Jump("/pages/login/download/download");
			}
		}

		// if (success) {
		// 	store.commit("user/SET_TOKEN", data);
		// 	setInvcode('');
		// 	Vue.prototype.Ws.reconnect();
		// 	if (type != 1) {
		// 		Jump("/pages/login/download/download");
		// 		// Jump({
		// 		// 	url: "/pages/login/bindIdCard/bindIdCard",
		// 		// 	mode: 3,
		// 		// });
		// 	}
		// 	let { data: { address, username, mail } } = await UserInfo();

		// 	// savaAccountlist({
		// 	// 	address: address,
		// 	// 	username: username,
		// 	// 	key: params.mnemonic,
		// 	// 	password: params.password,
		// 	// 	mail: mail,
		// 	// })
		// 	// 注册成功  跳转身份证绑定页
		// 	// if (type == 1) {
		// 	// 	Jump({
		// 	// 		url: "/pages/login/bindIdCard/bindIdCard",
		// 	// 		mode: 3,
		// 	// 	});
		// 	// } else {
		// 	// 	Jump("/pages/register/down");
		// 	// }
		// }
		return success;
	} catch (e) {
		console.log(e);
		return false;
	}
}


export async function UserInfo(params) {
	try {
		let {
			success,
			data,
			msg
		} = await request({
			url: "/Users/Info",
			method: "post",
			params,
			mask: false,
		});
		if (success) {
			await balance();
			data.ctime = data.ctime.replace("T", " ")
			data.lastLoginTime = data.lastLoginTime.replace("T", " ")
			store.commit("user/SET_USERINFO", data);
		}
		return {
			success,
			data
		}
	} catch (e) {
		console.log(e);
		return false;
	}
}

// 获取余额
export async function balance(params) {
	try {
		let {
			data,
			success
		} = await request({
			url: '/Users/Balance',
			method: "post",
			params,
			mask: false,
		})
		if (!success) return []
		let a = {};
		data.forEach(item => {
			const numA = BigInt(toWei(item.balance, 18))
			const numB = BigInt(toWei(item.lockbalance, 18))
			item.balance = fromWei(numA - numB, 18)
			a[item.coinid] = item
		})
		store.commit('user/SET_USERBALANCE', a);
		return data
	} catch (e) {
		return false
	}
}


// 检查账号是否注册
export async function IMEI_RegisterVerify(data) {
	try {
		let res = await request({
			url: '/Users/IMEI_RegisterVerify',
			method: "post",
			data,
		})
		return res
	} catch (e) {
		return false
	}
}



/**
 * 设置/重置/忘记密码
 * @param {*} params
 */
export async function SetPassword(params) {
	try {
		params.password = AESencrypt(params.password);
		let {
			success,
			data,
			code,
			msg
		} = await request({
			url: "/Users/SetPassword",
			method: "post",
			data: params,
		});
		Toast(msg);
		return success;
	} catch (e) {
		console.log(e);
		return false;
	}
}


/**
 * 设置支付密码
 * @param {*} params
 */
export async function SetPaypassword(params) {
	try {
		params.paypassword = AESencrypt(params.paypassword);
		let {
			success,
			data,
			code,
			msg
		} = await request({
			url: "/Users/SetPaypassword",
			method: "post",
			data: params,
		});
		Toast(msg);
		if (success) {
			await UserInfo();
		}
		return success;
	} catch (e) {
		console.log(e);
		return false;
	}
}

/**
 * 绑定邮箱
 * @param {*} params
 */
export async function setMail(params) {
	try {
		let {
			success,
			data,
			code,
			msg
		} = await request({
			url: "/users/setMail",
			method: "post",
			params,
		});
		Toast(msg);
		if (success) {
			await UserInfo();
		}
		return success;
	} catch (e) {
		console.log(e);
		return false;
	}
}

/**
 * 昵称和头像
 */
export async function SetHeadpic(params) {
	try {
		let {
			success,
			data,
			code,
			msg
		} = await request({
			url: "/Users/SetHeadpic",
			method: "post",
			data: params,
		});
		Toast(msg);
		if (success) {
			await UserInfo();
		}
		return success;
	} catch (e) {
		console.log(e);
		return false;
	}
}
/**
 * 昵称和头像
 */
export async function SetNickname(params) {
	try {
		let {
			success,
			data,
			code,
			msg
		} = await request({
			url: "/Users/SetNickname",
			method: "post",
			data: params,
		});
		Toast(msg);
		if (success) {
			await UserInfo();
		}
		return success;
	} catch (e) {
		console.log(e);
		return false;
	}
}

export async function GetMyEnergy(params) {
	try {
		let res = await request({
			url: "/Users/GetMyEnergy",
			method: "post",
			params,
		});
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}

export async function BindAntiWithdrawAddress(params) {
	try {
		let res = await request({
			url: "/Users/BindAntiWithdrawAddress",
			method: "post",
			params,
		});
		if (res.success) {
			await UserInfo()
		}
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}

export async function GetMyTeam(params) {
	try {
		let res = await request({
			url: "/Users/GetMyTeam",
			method: "post",
			params,
		});
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}



//我的战队
export async function CaptainInfo(params) {
	try {
		let res = await request({
			url: "/Users/CaptainInfo",
			method: "post",
			params,
		});
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}
//战队成员
export async function CaptainUsers(params) {
	try {
		let res = await request({
			url: "/Users/CaptainUsers",
			method: "post",
			params,
		});
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}

export async function CaptainEdit(data) {
	try {
		let res = await request({
			url: "/Users/CaptainEdit",
			method: "post",
			data,
		});
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}

export async function BindCountry(params) {
	try {
		let res = await request({
			url: "/Users/BindCountry",
			method: "post",
			params,
		});
		if (res.success) {
			await UserInfo()
		}
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}

export async function BindEmaill(params) {
	try {
		let res = await request({
			url: "/Users/BindEmaill",
			method: "post",
			params,
		});
		Toast(res.msg)
		if (res.success) {
			await UserInfo()
		}
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}
export async function BindPhone(params) {
	try {
		let res = await request({
			url: "/Users/BindPhone",
			method: "post",
			params,
		});
		Toast(res.msg)
		if (res.success) {
			await UserInfo()
		}
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}


//地址列表
export async function GetMyAddress() {
	try {
		let {
			success,
			data
		} = await request({
			url: "/Users/GetMyAddress",
			method: "post",
		});
		if (success) {
			if (!data) return
			let arr = {};
			data.forEach(item => {
				// 默认地址时
				if (item.isdefualt == 1) {
					store.commit('address/SET_DEFAULT', item.id)
				}
				arr[item.id] = item;
				// if(item.id==store.state.address.nowId){
				// 	store.commit('address/SET_NOWID', item.id)
				// }else{
				// 	store.commit('address/SET_NOWID', 0)
				// }
			})
			store.commit('address/SET_OBJ', arr)
			store.commit('address/SET_LIST', data)
			store.commit('address/SET_NOWID', 0)
		} else {
			store.commit('address/SET_DEFAULT', '')
			store.commit('address/SET_LIST', [])
		}
	} catch (e) {
		console.log(e);
		return false;
	}
}


//新增地址
export async function AddEditAddress(data) {
	try {
		let res = await request({
			url: "/Users/AddEditAddress",
			method: "post",
			data
		});
		if (res.success) {
			await GetMyAddress();
		}
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}

export async function DelAddress(data) {
	try {
		let res = await request({
			url: "/Users/DelAddress",
			method: "post",
			data
		});
		if (res.success) {
			// await GetMyAddress();
		}
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}


// 我的日收益统计
export async function TotalDayMy(params) {
	try {
		let res = await request({
			url: "/Users/TotalDayMy",
			method: "post",
			params
		});
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}

// 查询服务津贴收益明细
export async function CreditDetail(params) {
	try {
		let res = await request({
			url: "/Users/CreditDetail",
			method: "post",
			params
		});
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}

export async function BindAlipayaccount(params) {
	try {
		let res = await request({
			url: "/Users/BindAlipayaccount",
			method: "post",
			params
		});
		if (res.success) {
			UserInfo()
		}
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}

// 设置微信openid 
export async function SetWxopenid(params) {
	try {
		let res = await request({
			url: "/Users/SetWxopenid",
			method: "post",
			params
		});
		if (res.success) {
			UserInfo()
		}
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}

//  绑定收款账户
export async function addEditBank(data) {
	try {
		let res = await request({
			url: "/Users/addEditBank",
			// url: "/Trade/BindBankCard",
			method: "post",
			data
		});
		if (res.success) {
			// await UserInfo()
			await banklist()
		}
		return res;
	} catch (e) {
		return false;
	}
}

// 创建实名 订单
export async function BindIdcard(params) {
	try {
		let res = await request({
			url: '/Users/BindIdcard',
			method: "post",
			params
		})
		if (res.success) {
			await UserInfo()
		}
		return res
	} catch (e) {
		return false
	}
}

// 获取我的收款账户列表
export async function banklist(params) {
	try {
		const {
			success,
			msg,
			data,
			code
		} = await request({
			url: '/Users/banklist',
			method: "post",
			params
		})
		data.forEach(item => {
			if (item.cardtype == 1) store.commit('user/SET_BACKSELF', item);
			if (item.cardtype == 2) store.commit('user/SET_BACKPUBLIC', item)
		})
		store.commit('user/SET_USERBACKLIST', data)
		return {
			success,
			msg,
			data,
			code
		}
	} catch (e) {
		return false
	}
}

// 我的合伙人订单列表
export async function hhrpage(params) {
	try {
		const {
			success,
			msg,
			list,
			code
		} = await request({
			url: '/Users/hhrpage',
			method: "post",
			params
		})
		return {
			success,
			msg,
			list,
			code
		}
	} catch (e) {
		return false
	}
}


// 我的 公司 下的合伙人
export async function hhrpageFromCompy(params) {
	try {
		const {
			success,
			msg,
			list,
			code,
			data,
			totalCount
		} = await request({
			url: '/Users/hhrpageFromCompy',
			method: "post",
			params
		})
		return {
			success,
			msg,
			list,
			code,
			data,
			totalCount
		}
	} catch (e) {
		return false
	}
}

// 合伙人 发货列表
export async function hhrwaybillpage(params) {
	try {
		const {
			success,
			msg,
			list,
			code
		} = await request({
			url: '/Users/hhrwaybillpage',
			method: "post",
			params
		})
		return {
			success,
			msg,
			list,
			code
		}
	} catch (e) {
		return false
	}
}

// 设置合伙人订单收货地址
export async function myhhrorder_setaddr(params) {
	try {
		const {
			success,
			msg,
			data,
			code
		} = await request({
			url: '/Users/myhhrorder_setaddr',
			method: "post",
			params
		})
		return {
			success,
			msg,
			data,
			code
		}
	} catch (e) {
		return false
	}
}

// 我的业绩  -- 打印机
export async function mytotal() {
	try {
		const {
			success,
			msg,
			data,
			code
		} = await request({
			url: '/Users/mytotal',
			method: "post",
		})
		return {
			success,
			msg,
			data,
			code
		}
	} catch (e) {
		return false
	}
}


// 我的业绩  -- AI美食机
export async function mytotalW3() {
	try {
		const {
			success,
			msg,
			data,
			code
		} = await request({
			url: '/Users/mytotalW3',
			method: "post",
		})
		return {
			success,
			msg,
			data,
			code
		}
	} catch (e) {
		return false
	}
}
// 我的团队 
export async function myteam(params) {
	try {
		const res = await request({
			url: '/Users/myteam',
			method: "post",
			data: params
		})
		return res
	} catch (e) {
		return false
	}
}
// 我的团队 
export async function myteam_Dls(params) {
	try {
		const res = await request({
			url: '/Users/myteam_Dls',
			method: "post",
			data: params
		})
		return res
	} catch (e) {
		return false
	}
}
// 代理月业绩总计
export async function mytotalDay_dls(params) {
	try {
		const res = await request({
			url: '/Users/mytotalDay_dls',
			method: "post",
			data: params
		})
		return res
	} catch (e) {
		return false
	}
}

// 我的团队 --- 打印机
export async function teamtotal(params) {
	try {
		const res = await request({
			url: '/Users/teamtotal',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

// 绑定关系
export async function bindInvcode(params) {
	try {
		const {
			success,
			msg,
			data,
			code
		} = await request({
			url: '/Users/bindInvcode',
			method: "post",
			params
		})
		Toast(msg);
		if (success) {
			await UserInfo();
		}
		return {
			success,
			msg,
			data,
			code
		}
	} catch (e) {
		return false
	}
}

// 用户确认信息 编辑----2023年11月14日10:09:57
export async function tokenAddEdit(data) {
	try {
		const {
			success,
			msg,
			data: data2,
			code
		} = await request({
			url: '/Users/tokenAddEdit',
			method: "post",
			data
		})
		Toast(msg);
		return {
			success,
			msg,
			data,
			code
		}
	} catch (e) {
		return false
	}
}

// 用户确认信息 获取 2023年11月14日10:11:23
export async function mytoken(params) {
	try {
		const {
			success,
			msg,
			data,
			code
		} = await request({
			url: '/Users/mytoken',
			method: "post",
			params
		})
		return {
			success,
			msg,
			data,
			code
		}
	} catch (e) {
		return false
	}
}

// 我的公司团队业绩
export async function compyTeam(params) {
	try {
		const {
			success,
			msg,
			list,
			code
		} = await request({
			url: '/Users/compyTeam',
			method: "post",
			params
		})
		return {
			success,
			msg,
			list,
			code
		}
	} catch (e) {
		return false
	}
}

export async function mycompy(params) {
	try {
		const {
			success,
			msg,
			data,
			code
		} = await request({
			url: '/Payment/mycompy',
			method: "post",
			params
		})
		return {
			success,
			msg,
			data,
			code
		}
	} catch (e) {
		return false
	}
}

// 设置用户标签
export async function SetUserTag(data) {
	try {
		let res = await request({
			url: '/Users/SetUserTag',
			method: "post",
			data,
		})
		return res
	} catch (e) {
		return false
	}
}